home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / UPC12BS2.ZIP / UTIL / GENSIG.C < prev    next >
C/C++ Source or Header  |  1993-09-27  |  22KB  |  575 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       Program:    gensig.c             24 July 1991                */
  3. /*       Author:     Andrew H. Derbyshire                             */
  4. /*       Address:    ahd@kew.com                                      */
  5. /*       Function:   Append a random quote to an electronic           */
  6. /*                   mail signature file                              */
  7. /*       Language:   Borland C++ 2.0 (in ANSI C mode)                 */
  8. /*       Arguments:  Name of file with fixed text input               */
  9. /*                   Name of file with variable quotes                */
  10. /*                   Name of file to be written                       */
  11. /*--------------------------------------------------------------------*/
  12.  
  13. /*--------------------------------------------------------------------*/
  14. /*                          RCS Information                           */
  15. /*--------------------------------------------------------------------*/
  16.  
  17. /*
  18.  *    $Id: gensig.c 1.4 1993/09/27 04:04:06 ahd Exp $
  19.  *
  20.  *    Revision history:
  21.  *    $Log: gensig.c $
  22.  * Revision 1.4  1993/09/27  04:04:06  ahd
  23.  * Suppress compiler warning message
  24.  *
  25.  * Revision 1.3  1993/04/11  00:33:54  ahd
  26.  * Global edits for year, TEXT, etc.
  27.  *
  28.  * Revision 1.2  1993/03/06  23:04:54  ahd
  29.  * Alter message to include output files
  30.  *
  31.  * Revision 1.1  1992/11/15  04:29:22  ahd
  32.  * Initial revision
  33.  *
  34.  */
  35.  
  36. static char rcsid[] = "$Id: gensig.c 1.4 1993/09/27 04:04:06 ahd Exp $";
  37.  
  38. /*--------------------------------------------------------------------*/
  39. /*                       Standard include files                       */
  40. /*--------------------------------------------------------------------*/
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <io.h>
  45. #include <direct.h>
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48. #include <string.h>
  49. #include <time.h>
  50.  
  51. /*--------------------------------------------------------------------*/
  52. /*                    UUPC/extended include files                     */
  53. /*--------------------------------------------------------------------*/
  54.  
  55. #include "lib.h"
  56. #include "timestmp.h"
  57.  
  58. /*--------------------------------------------------------------------*/
  59. /*                            Local macros                            */
  60. /*--------------------------------------------------------------------*/
  61.  
  62. #define bitsper(s) (8 * (sizeof s))
  63.  
  64. #define bitflag(x, s) (1 << ((bitsper(s) - 1) - x))
  65.  
  66. #define biton( s, offset ) \
  67.                (( s[ (size_t) (offset / bitsper(*s)) ] ) & \
  68.                 bitflag(offset % bitsper( *s ), *s))
  69.  
  70. #define bitoff( s, offset) (!biton(s, offset))
  71.  
  72. /*--------------------------------------------------------------------*/
  73. /*                    Internal function prototypes                    */
  74. /*--------------------------------------------------------------------*/
  75.  
  76. static void usage( void );
  77.  
  78. static long chooseit( struct stat *current_status,
  79.        const char *lookaside,
  80.                  const char *quoteused,
  81.                  const char *fname ,
  82.                  const char *target);
  83.  
  84. static long getquote( const char *data, const char *target);
  85.  
  86. static void CopyQuote( const char *fname, long where, FILE *stream);
  87.  
  88. static void CopyFixed( const char *fname, FILE *stream );
  89.  
  90. static long chooseavailable( const char *quoteused, long quotes );
  91.  
  92. currentfile();
  93.  
  94. /*--------------------------------------------------------------------*/
  95. /*    m a i n                                                         */
  96. /*                                                                    */
  97. /*    main program                                                    */
  98. /*--------------------------------------------------------------------*/
  99.  
  100. void main( int argc, char **argv)
  101. {
  102.  
  103.    long where;
  104.    FILE *stream;
  105.  
  106.    banner( argv );
  107.  
  108. /*--------------------------------------------------------------------*/
  109. /*                  Validate the number of arguments                  */
  110. /*--------------------------------------------------------------------*/
  111.  
  112.       if ( argc !=  4 )
  113.          usage();
  114.  
  115. /*--------------------------------------------------------------------*/
  116. /*    Determine the number of the quotes available, and then          */
  117. /*    select one                                                      */
  118. /*--------------------------------------------------------------------*/
  119.  
  120.       where = getquote( argv[2], argv[3] );
  121.  
  122. /*--------------------------------------------------------------------*/
  123. /*                      Open up our output file                       */
  124. /*--------------------------------------------------------------------*/
  125.  
  126.       stream = fopen( argv[3] , "w");
  127.       if ( stream == NULL )
  128.       {
  129.          perror( argv[3] );
  130.          exit(1);
  131.       }
  132.  
  133. /*--------------------------------------------------------------------*/
  134. /*           Copy the fixed and variable parts of the file            */
  135. /*--------------------------------------------------------------------*/
  136.  
  137.       CopyFixed( argv[1], stream );
  138.       CopyQuote( argv[2], where, stream );
  139.  
  140. /*--------------------------------------------------------------------*/
  141. /*                   Close up and return to caller                    */
  142. /*--------------------------------------------------------------------*/
  143.  
  144.       fclose( stream );
  145.       exit( 0 );
  146.  
  147. } /* main */
  148.  
  149. /*--------------------------------------------------------------------*/
  150. /*    u s a g e                                                       */
  151. /*                                                                    */
  152. /*    Report program usage and then exit                              */
  153. /*--------------------------------------------------------------------*/
  154.  
  155. static void usage( void )
  156. {
  157.    printf("Usage:\tgensig\taddr-file quote-file output-file\n");
  158.    printf("\taddr-file\tFixed portion of signature file\n");
  159.    printf("\tquote-file\tFile of quotes, separated by delimiter lines\n");
  160.    printf("\toutput-file\tOutput file with fixed portion and single quote\n");
  161.    exit( 2 );
  162. } /* usage */
  163.  
  164. /*--------------------------------------------------------------------*/
  165. /*    g e t q u o t e                                                 */
  166. /*                                                                    */
  167. /*    Select a quote to process                                       */
  168. /*--------------------------------------------------------------------*/
  169.  
  170. static long getquote( const char *data, const char *target)
  171. {
  172.    struct stat current_status;
  173.    long where;
  174.  
  175.    char lookaside[FILENAME_MAX];
  176.    char quoteused[FILENAME_MAX];
  177.    char drive[FILENAME_MAX],
  178.         dir[FILENAME_MAX],
  179.         file[FILENAME_MAX],
  180.         ext[FILENAME_MAX];
  181.  
  182. /*--------------------------------------------------------------------*/
  183. /*       Get size and data information on the quotes data file        */
  184. /*--------------------------------------------------------------------*/
  185.  
  186.    if(stat((char *) data, ¤t_status ) <0)
  187.    {
  188.       perror( data );         /* If no data file, panic gracefully   */
  189.       exit( 3 );
  190.    } /* if */
  191.  
  192. /*--------------------------------------------------------------------*/
  193. /*       Build the lookaside file name from the data file name        */
  194. /*--------------------------------------------------------------------*/
  195.  
  196. #ifdef __TURBOC__
  197.    fnsplit( data, drive, dir, file, ext);
  198.    fnmerge( lookaside, drive, dir, file, ".las");
  199.    fnmerge( quoteused, drive, dir, file, ".qus");
  200. #else
  201.    _splitpath( data, drive, dir, file, ext);
  202.    _makepath( lookaside, drive, dir, file, ".las");
  203.    _makepath( quoteused, drive, dir, file, ".qus");
  204. #endif /* __TURBOC__ */
  205.  
  206. /*--------------------------------------------------------------------*/
  207. /*    Now get the location of the quote; if it fails the first        */
  208. /*    time, then the lookaside buffer is updated and we can try       */
  209. /*    again                                                           */
  210. /*------------------------